package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 增加美女亲密度
 */
public class UseAddBeautyIntimacy extends BaseUseGoods {

	public UseAddBeautyIntimacy(GamePlayer player, GoodsInfo goodsInfo, long param) {
		super(player, goodsInfo, param);
	}

	@Override
	int checkUse(long count) {
		int occupation = goodsInfo.getParamList().get(1).intValue();
		if(occupation >= 0){
			UserBeauty userBeauty = player.getModule(BeautyModule.class).getRandomBeauty(occupation);
			if(null == userBeauty){
				return GameErrorCode.E_BAG_ITEM_USE_NO_OCCUPATION_BEAUTY;
			}
		}
		return 0;
	}

	@Override
	UseGoodsResult executeUse(long count) {
		UseGoodsResult result = new UseGoodsResult();
		int isRandomIntimacy = goodsInfo.getParamList().get(0).intValue();
		int occupation = goodsInfo.getParamList().get(1).intValue();
		int beautyId = (int) param;
		UserBeauty userBeauty = null;
		BeautyModule beautyModule = player.getModule(BeautyModule.class);
		if(-1 == occupation){
			if(0 == param){
				result.setRet(GameErrorCode.E_BAG_ITEM_USE_NO_SET_BEAUTY);
				return result;
			}
			userBeauty = beautyModule.getUserBeauty(beautyId);
			if(null == userBeauty){
				result.setRet(GameErrorCode.E_BEAUTY_NO_FOUND);
				return result;
			}
		}
		Property property = new Property();
		List<UserBeauty> syncList = new ArrayList<>();
		// 标记美女被加了多少次
		Map<Integer, Integer> beautyAddNumMap = new HashMap<>();

		for(int i = 0; i < count; i++) {
			int addIntimacy = 0;
			if(0 == isRandomIntimacy){
				addIntimacy = GoodsMgr.getAddRandomValue(goodsInfo).intValue();
			}else{
				addIntimacy = isRandomIntimacy;
			}
			UserBeauty randomBeauty = userBeauty;
			if(null == randomBeauty) {
				randomBeauty = beautyModule.getRandomBeauty(occupation);
			}
			if(null != randomBeauty) {

				property.addProperty(randomBeauty.getBeautyId(), BigInteger.valueOf(addIntimacy));
				if(!syncList.contains(randomBeauty)){
					syncList.add(randomBeauty);
				}

				if (!beautyAddNumMap.containsKey(randomBeauty.getBeautyId())) {
					beautyAddNumMap.put(randomBeauty.getBeautyId(), 0);
				}
				beautyAddNumMap.put(randomBeauty.getBeautyId(), beautyAddNumMap.get(randomBeauty.getBeautyId()) + 1);
			}
		}

		// 统一日志及逻辑处理 防止因道具一次使用太多导致逻辑冗余处理及日志冗余
		for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
			int addBeautyId = entry.getKey();
			int addValue = entry.getValue().intValue();
			UserBeauty addUserBeauty = beautyModule.getUserBeauty(addBeautyId);

			if (addUserBeauty != null)  {
				int oldValue = addUserBeauty.getTotalIntimacy();
				//增加美女亲密度
				beautyModule.addIntimacy(addUserBeauty, addValue, eLogMoneyType.Beauty.getValue(), eLogMoneyType.BeautyUseAddIntimacyGoods.getValue());

				LogMgr.addLogUseAddAttributeGoods(player.getUserId(), goodsInfo.getGoodsId(), goodsInfo.getType(), addUserBeauty.getBeautyId(), addValue,
						BigInteger.valueOf(oldValue), BigInteger.valueOf(addUserBeauty.getTotalIntimacy()), beautyAddNumMap.getOrDefault(addBeautyId, 0));
			}
		}


		if(syncList.size() > 0){
			beautyModule.syncUserBeauty(syncList, false);
			beautyModule.onBeautyListChange(syncList);
		}
		result.setReward(PropertyHelper.parsePropertyToString(property));
		return result;
	}
}
